import { _decorator, Component, director, EditBox, Node } from 'cc';
import { NetWorkManager } from '../Global/NetWorkManager';
import { ApiMsgEnum } from '../Common';
import DataManager from '../Global/DataManager';
import { SceneEnum } from '../Enum';
const { ccclass, property } = _decorator;

@ccclass('LoginMgr')
export class LoginMgr extends Component {
    input:EditBox;
    protected onLoad(): void {
        this.input = this.getComponentInChildren(EditBox);
        director.preloadScene(SceneEnum.Hall,()=>{
            console.log("Battle preload success");
        })
    }
    async start() {
        await NetWorkManager.Instance.connect();
    }

    async handleClick(){
       if(!NetWorkManager.Instance.isConnect){
           console.log("未连接");
           await NetWorkManager.Instance.connect();
           return;
       } 

       const nickname = this.input.string;
       if(!nickname){
           console.log("请输入昵称");
           return; 
       }

       const {success,error,res} = await NetWorkManager.Instance.callApi(ApiMsgEnum.ApiPlayerJoin,{nickname});
        if(!success){
            console.log(error);
            return;
        }
        DataManager.Instance.myPlayerId = res.player.id;
        
        director.loadScene(SceneEnum.Hall);
    }

    update(deltaTime: number) {
        
    }
}


